import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "element-ui/lib/theme-chalk/index.css";
import {
  Button,
  Autocomplete,
  Select,
  Input,
  Row,
  Col,
  Collapse,
  CollapseItem,
  Image,
  Link,
  Card,
  Progress,
  Radio,
  RadioGroup,
  RadioButton,
  Divider,
  Breadcrumb,
  BreadcrumbItem,
  Carousel,
  CarouselItem,
  Rate,
  Upload,
  Dialog,
  Tabs,
  TabPane,
  Icon,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Menu,
  Submenu,
  MenuItem,
  Table,
  TableColumn,
  Loading,
  MenuItemGroup,
  MessageBox,
  Pagination,
  Message,
  Form,
  Option,
  OptionGroup,
  FormItem,
  Checkbox,
  CheckboxButton,
  CheckboxGroup,
  Tag,
  Popconfirm,
  DatePicker,
  Cascader,
  Tooltip,
  CascaderPanel,
  ColorPicker,
  Backtop,
  Switch,
  PageHeader,
  Badge
} from "element-ui";
import VueStorage from "vue-ls";
Vue.use(VueStorage);
// require("./mock"); //引入mock数据，关闭则注释该行

import echarts from "echarts";

Vue.prototype.$echarts = echarts;
// import CollapseTransition from "element-ui/lib/transitions/collapse-transition";
import "element-ui/lib/theme-chalk/base.css";
import animate from "animate.css";
import VueClipboard from "vue-clipboard2";
VueClipboard.config.autoSetContainer = true;

import VueJsonp from "vue-jsonp";
// import { getToken } from "./utils/auth";
Vue.use(animate);
Vue.use(VueClipboard);
Vue.use(VueJsonp);
Vue.config.productionTip = false;
// Vue.component(CollapseTransition.name, CollapseTransition);
Vue.use(Button)
  .use(Option)
  .use(Cascader)
  .use(CascaderPanel)
  .use(ColorPicker)
  .use(Autocomplete)
  .use(Badge)
  .use(Switch)
  .use(Backtop)
  .use(PageHeader)
  .use(Tooltip)
  .use(Tag)
  .use(DatePicker)
  .use(Checkbox)
  .use(CheckboxButton)
  .use(CheckboxGroup)
  .use(OptionGroup)
  .use(Form)
  .use(Table)
  .use(Select)
  .use(FormItem)
  .use(TableColumn)
  .use(Loading)
  .use(Pagination)
  .use(Row)
  .use(Col)
  .use(Collapse)
  .use(CollapseItem)
  .use(Link)
  .use(Menu)
  .use(Submenu)
  .use(MenuItem)
  .use(MenuItemGroup)
  .use(Image)
  .use(Card)
  .use(Progress)
  .use(Radio)
  .use(RadioGroup)
  .use(RadioButton)
  .use(Divider)
  .use(BreadcrumbItem)
  .use(Breadcrumb)
  .use(Carousel)
  .use(CarouselItem)
  .use(Rate)
  .use(Dialog)
  .use(Upload)
  .use(Tabs)
  .use(TabPane)
  .use(Icon)
  .use(Input)
  .use(Dropdown)
  .use(DropdownMenu)
  .use(Popconfirm)
  .use(DropdownItem);
Vue.prototype.$message = Message;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$loading = Loading.service;

Vue.prototype.$notify = Notification;

// 路由守卫
const whiteList = [
  "/login/login",
  "/login/auditlogin",
  "/login/forgetpass",
  "/optimGoods/optimBank",
  "/optimGoods/nowList",
  "/optimGoods/siftGoods",
  "/optimGoods/meatlive",
  "/optimGoods/widge",
  "/optimGoods/goodsDetails",
  "/optimGoods/widge/widgepic"
]; // 白名单

router.beforeEach(async (to, from, next) => {
  // 根据meta设置头部title
  // console.log("store", store.state);

  window.document.title = to.meta.title;

  const hasToken = Vue.ls.get("token");
  // console.log("token", Vue.ls.get("token"));

  if (hasToken) {
    // console.log(to);
    if (to.path === "/login") {
      if (to.query.active === "register") {
        next();
      } else {
        next({
          path: "/optimGoods/siftGoods"
        });
      }
    } else {
      next();
      if (to.path === "/") {
        next({
          path: "/optimGoods/siftGoods"
        });
      }
      // 权限的检测
      // if (store.state.auth.routes.length == 0) {
      //   // 调用store的初始化菜单的功能
      //   store
      //     .dispatch("auth/initClientMenu", hasToken.user.id)
      //     .then(() => {
      //       // console.log('跳转到...', from.path, to.path);
      //       next(to.path);
      //     })
      //     .catch(() => {
      //       next({
      //         path: "/login"
      //       });
      //     });
      // } else {
      //   next();
      // }
    }
    // if(to.path === "/login"){

    // }
  } else {
    /* has no token*/
    // console.log(store.state);
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next();
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      // next(`/login?redirect=${to.path}`)   ?后面是来自哪个地址跳转过来的
      next(`/optimGoods/siftGoods`);
    }
  }
});
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
